package controllers

import "github.com/astaxie/beego"
import (
	"encoding/json"
	"git.oschina.net/fanbuchi/timemaker/models"
)

// mobile app version manage
type VersionController struct {
	beego.Controller
}

// @Author killerzhangsan
// @Title publicVersion
// @Description public mobile app version
// @Param   client_id     header    string  true        "The client_id"
// @Param   verison    body    json  true        "The info about public mobile app version"
// @Success 200 ok
// @router /publicVersion [post]
func (this *VersionController) PublicVersion() {
	var version models.Version
	json.Unmarshal(this.Ctx.Input.RequestBody, &version)

	err := models.PublicVersion(&version)
	var objectStatus models.ObjectStatus
	if err != nil {
		objectStatus = models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
	} else {
		objectStatus = models.ObjectStatus{Status: models.Ok, Desc: "version public success"}
	}
	this.Data[dataformat] = objectStatus
	this.ServeJson()
}

// @Author killerzhangsan
// @Title checkVersion
// @Description check  Version valid
// @Param   client_id     header    string  true        "The client_id"
// @Param    version_id    query    string  true        "The version_id for key"
// @Param    version_name    query    string  true        "The version_name for key"
// @Success 200 ok
// @router /checkVersion [get]
func (this *VersionController) CheckVersion() {
	var v models.Version
	this.ParseForm(&v)

	err := models.CheckVersion(&v)
	var objectStatus interface{}
	if err != nil {
		objectStatus = models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
	} else {
		objectStatus = v
	}
	this.Data[dataformat] = objectStatus
	this.ServeJson()
}

// @Author killerzhangsan
// @Title lastVersion
// @Description Last All Version
// @Param   client_id     header    string  true        "The client_id"
// @Param    version_name    query    string  true        "The version_name for key"
// @Success 200 ok
// @router /lastVersion [get]
func (this *VersionController) LastVersion() {
	var v models.Version
	this.ParseForm(&v)

	err := models.LastVersion(&v)
	var objectStatus interface{}
	if err != nil {
		objectStatus = models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
	} else {
		objectStatus = v
	}
	this.Data[dataformat] = objectStatus
	this.ServeJson()
}

// @Author killerzhangsan
// @Title listVersion
// @Description List All Version
// @Param   client_id     header    string  true        "The client_id"
// @Param    version_name    query    string  true        "The versionId for key"
// @Param    page    query    uint  true        "The page for key"
// @Param    page_size    query    uint  true        "The page_size for key"
// @Success 200 ok
// @router /listVersion [get]
func (this *VersionController) ListVersion() {
	var v models.Version
	var page models.Pages
	this.ParseForm(&v)
	this.ParseForm(&page)
	version, err := models.ListVersion(v.VersionName, page)
	var objectStatus interface{}
	if err != nil {
		objectStatus = models.ObjectStatus{Status: models.ParamError, Desc: err.Error()}
	} else {
		objectStatus = version
	}
	this.Data[dataformat] = objectStatus
	this.ServeJson()
}

// @Author killerzhangsan
// @Title version
// @Description app version about
// @Param   client_id     header    string  true        "The client_id"
// @router / [get]
func (this *VersionController) Get() {
	this.Data[dataformat] = models.ObjectStatus{Status: models.Ok, Desc: "api version group valid"}
	this.ServeJson()
}
